package com.matrix.java.chap_07;

import com.matrix.java.util.ArrayUtil;

import java.util.Arrays;
import java.util.Scanner;

/*
    计算数字的出现次数
    当元素为整数且元素范围较小时,可用i当做数组元素,计数时++即可.
 */
public class Ex7_03 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] counts = new int[100];
        System.out.println("Enter the integers between 1 and 100: ");
        int number = input.nextInt();
        while (number != 0) {
            if (number <= 100 && number >= 0) {
                counts[number - 1]++;
            }
            number = input.nextInt();
        }
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] == 1) {
                System.out.println(i + " occurs " + counts[i] + " time");
            } else if (counts[i] > 1) {
                System.out.println(i + " occurs " + counts[i] + " time");
            }
        }
    }
    /*public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arrIn = new int[9999];
        int arrNum =0;
        System.out.println("Enter the integers between 1 and 100: ");
        for (int i = 0;; i++) {
            int anInt = input.nextInt();
            if (anInt==0){
                break;
            }
            arrIn[i] = anInt;
            arrNum++;
        }
        input.close();
        int[] arr = new int[arrNum];
        System.arraycopy(arrIn,0,arr,0,arrNum);
        int[] arrayUnrepeated = ArrayUtil.arrayUnrepeated(arr);
        int[] elementCount = ArrayUtil.arrayElementCount(arr);
        for (int i = 0; i < arrayUnrepeated.length; i++) {
            if (elementCount[i]>1) {
                System.out.println(arrayUnrepeated[i] + " occurs "+
                        elementCount[i] +" times");
            }else {
                System.out.println(arrayUnrepeated[i] + " occurs "+
                        elementCount[i] +" time");
            }
        }
    }*/
}
